﻿using APPFrameworkStudy.DAL;
using APPFrameworkStudy.Domain;
using APPFrameworkStudy.Repository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace APPFrameworkStudy.Services
{
    /// <summary>
    /// 服务提供粗粒度接口供表现层调用
    /// </summary>
    public class OrderServices : BaseServices
    {

        public OrderServices()
        {
            //APPFrameworkStudy.DAL.SqlServer.OrderDataMapper test = this.DataContext.GetDataMapper<Order>() as APPFrameworkStudy.DAL.SqlServer.OrderDataMapper;
            
        }

        /// <summary>
        /// 生成订单
        /// </summary>
        /// <returns></returns>
        public Order CreateOrder(Order order)
        {
            
            //初始化订单信息
            order.ShipAmount = order.GetOrderAmount();
            order.CreateTime = DateTime.Now;
            order.Number = Guid.NewGuid().ToString();
            //保存订单
            this.DataContext.Create<Order>(order);
            return order;
        }

        public bool DeleteOrder(Order order)
        {
            return this.DataContext.Delete<Order>(order);
        }

        public bool UpdateOrder(Order order)
        {
            return this.DataContext.Update<Order>(order);
        }

        /// <summary>
        /// 获取今日订单
        /// </summary>
        /// <returns></returns>
        public IList<Order> GetTodayOrders()
        {
            //查询条件 今日所有订单
            Expression<Func<Order, bool>> where = order => order.CreateTime > DateTime.Today;
            return this.DataContext.GetByCriteria<Order>(where);
        }

        /// <summary>
        /// 获取所有订单并按时间倒叙排序
        /// </summary>
        /// <returns></returns>
        public IList<Order> GetAllOrdersByOrder()
        {
            IOrderAndInclude<Order> query = new OrderAndInclude<Order>(this.DataContext.GetQueryable<Order>());
            //按照时间倒序排序，并且贪婪模式获取外键数据
            query.Desc<DateTime>(o => o.CreateTime).Include(o => o.OrderItems);
            return this.DataContext.GetAll<Order>(query.Queryable);
        }

        /// <summary>
        /// 获取所有订单信息
        /// </summary>
        /// <returns></returns>
        public IList<Order> GetAllOrders()
        {
            return this.DataContext.GetAll<Order>();
        }

        /// <summary>
        /// 获取排序分页数据
        /// </summary>
        /// <returns></returns>
        public IList<Order> GetPageByOrder(int pageIndex,int pageSize,out int total)
        {
            Expression<Func<Order, List<Product>>> include1 = tmp => tmp.OrderItems;
            IOrderAndInclude<Order> query = new OrderAndInclude<Order>(this.DataContext.GetQueryable<Order>());
            //时间降序，然后再ID降序，再获取外键数据
            query.Desc<DateTime>(o => o.CreateTime).ThenDesc<int>(o => o.ID).Include(o => o.OrderItems);
            return this.DataContext.GetPage<Order>(query.Queryable, pageIndex, pageSize, out total);
        }

        /// <summary>
        /// 获取分页数据
        /// </summary>
        /// <returns></returns>
        public IList<Order> GetPage(int pageIndex, int pageSize, out int total)
        {
            return this.DataContext.GetPage<Order>(pageIndex, pageSize, out total);
        }
    }
}
